﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Windows.Controls;
using System.Xml.Linq;
using ThreeByte.ActiveDeck.Controls;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.Config;
using System.Windows.Data;
using System.Windows;
using ThreeByte.Converters;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;

namespace ThreeByte.ActiveDeck.AddIn.Content.Controls
{

    public class DesignerVideo : DesignerControl
    {
        [Browsable(false)]
        public VideoResource Video {
            get;
            private set;
        }

        private bool _fill;
        [Category("Layout")]
        public bool Fill {
            get {
                return _fill;
            }
            set {
                _fill = value;
                NotifyPropertyChanged(@"Fill");
            }
        }

        private bool _loop;
        public bool Loop {
            get {
                return _loop;
            }
            set {
                _loop = value;
                NotifyPropertyChanged(@"Loop");
            }
        }

        private Orientation _layoutOrientation = Orientation.Portrait;
        
        [Category("Layout")]
        public Orientation LayoutOrientation {
            get {
                return _layoutOrientation;
            }
            set {
                _layoutOrientation = value;
                NotifyPropertyChanged("LayoutOrientation");
            }
        }


        public DesignerVideo(string mediaPath, string pathRoot, PixelSpace pos)
            : base(pos) {

                Video = new VideoResource(System.IO.Path.GetFileNameWithoutExtension(mediaPath), mediaPath, pathRoot);

        }

        //Allows restoring videos with identical Guids
        public DesignerVideo(Guid resourceId, string mediaPath, string pathRoot, PixelSpace pos)
            : base(pos) {

            Video = new VideoResource(resourceId, System.IO.Path.GetFileNameWithoutExtension(mediaPath), mediaPath, pathRoot);

        }

        public DesignerVideo(XElement node, Dictionary<Guid, GraphicResource> resources) {

            //Get the media path
            XElement videoNode = node.Element(ns + "Video");

            XElement mediaNode = videoNode.Element(ns + "Media");
            string pathRoot = Globals.ThisAddIn.GetCurrentPresentationPath();
            Video = VideoResource.FromXml(mediaNode, pathRoot);

            if(mediaNode.Attribute("Loop") != null) {
                Loop = bool.Parse(mediaNode.Attribute("Loop").Value);
            }

            if(mediaNode.Attribute("Orientation") != null) {
                LayoutOrientation = (Orientation)(Enum.Parse(typeof(Orientation), mediaNode.Attribute("Orientation").Value));
            }

            //Get the Position/Size
            int top = int.Parse(videoNode.Attribute("Top").Value);
            int left = int.Parse(videoNode.Attribute("Left").Value);
            int width = int.Parse(videoNode.Attribute("Width").Value);
            int height = int.Parse(videoNode.Attribute("Height").Value);

            //Update this object
            DefaultPixelSpace = new PixelSpace() { W = 150, H = 40, X = 300, Y = 300 };
            CurrentPixelSpace = new PixelSpace() { X = left, Y = top, W = width, H = height };
        }


        public override XElement ToXml() {
            XElement node = base.ToXml();
            XElement mediaNode = new XElement(ns + "Video");
            mediaNode.Add(Video.ToXml());
            mediaNode.Add(PixelSpaceAsAttributes(CurrentPixelSpace));
            mediaNode.Add(new XAttribute("Loop", Loop));
            mediaNode.Add(new XAttribute("Orientation", LayoutOrientation));
            node.Add(mediaNode);
            return node;
        }

        public override XElement GetPresentationXml(PixelTranslator translation = null) {
            XElement node = new XElement(ns + "Video");
            PixelSpace pos = CurrentPixelSpace;
            if(translation != null) {
                pos = translation.Translate(pos);
            }
            node.Add(PixelSpaceAsAttributes(pos));
            node.Add(new XAttribute("Loop", Loop));
            node.Add(new XAttribute("Orientation", LayoutOrientation));

            XElement mediaNode = new XElement(ns + "Media");
            mediaNode.Add(new XAttribute("ResourceID", Video.ID));
            node.Add(mediaNode);

            return node;
        }

        public override List<VideoResource> GetMediaResources() {
            List<VideoResource> resources = new List<VideoResource>();
            resources.Add(Video);

            return resources;
        }

        public static DesignerVideo CreateFromMediaFile(string mediaPath, string pathRoot, Point insertAt = default(Point)) {

            //TODO: Determine image size?
            PixelSpace pos = new PixelSpace() { X = (int)(insertAt.X), Y = (int)(insertAt.Y), H = 150, W = 200 };

            return new DesignerVideo(mediaPath, pathRoot, pos);

        }
        
    }
}
